﻿Imports DotNetNuke
Imports DotNetNuke.UI.Utilities

Imports Lucene.Net.Documents

Imports Aricie.DNN.Services
Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports DotNetNuke.Services.Localization
Imports System.Linq
Imports Lucene.Net.Search
Imports Lucene.Net.Index

Namespace Aricie.DNN.Modules.LuceneSearch.UI
    Partial Public Class FieldDropDownListFilter
        Inherits FieldListFilterBase

#Region "Private Members"

        Private _nullLabel As String
        Private _displayEmptyItem As Boolean = True
        Private _defaultItem As String

        Private Shared ReadOnly MultiFieldSeparator As String = "___!MF!___"
#End Region

#Region "Public Property"


        Public Overrides Property Enabled() As Boolean
            Get
                Return Me.ddlListField.Enabled
            End Get
            Set(ByVal value As Boolean)
                Me.ddlListField.Enabled = value
            End Set
        End Property

        Public Overridable Property AutoPostBack() As Boolean
            Get
                Return Me.ddlListField.AutoPostBack
            End Get
            Set(ByVal value As Boolean)
                Me.ddlListField.AutoPostBack = value
            End Set
        End Property

        Public Property NullLabel() As String
            Get
                Return _nullLabel
            End Get
            Set(ByVal value As String)
                _nullLabel = value
            End Set
        End Property

        Public Property DisplayEmptyItem() As Boolean
            Get
                Return _displayEmptyItem
            End Get
            Set(ByVal value As Boolean)
                _displayEmptyItem = value
            End Set
        End Property

        Public Property DefaultItem() As String
            Get
                Return _defaultItem
            End Get
            Set(ByVal value As String)
                _defaultItem = value
            End Set
        End Property

#End Region

#Region "Event Handlers"

        Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
            If Not Me.IsPostBack Then
                Me.BindLayout()
            End If
        End Sub

        Private Sub ddlListField_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlListField.SelectedIndexChanged
            If Me.AutoPostBack Then
                Me.OnFilterChanged()
            End If
        End Sub

#End Region
        Public Sub New()
            ' par défaut, on est en mode filtre
            QueryMode = UI.QueryMode.Filter
        End Sub

        Public Sub ForceLayout()
            BindLayout()
        End Sub

#Region "Overrides"

        Protected Overrides Property FieldValue() As String
            Get
                Dim toReturn As String = ""
                If Not String.IsNullOrEmpty(Me.ddlListField.SelectedValue) Then
                    toReturn = ddlListField.SelectedValue.ToString
                End If
                Return toReturn
            End Get
            Set(ByVal value As String)
                If ddlListField.Items.FindByValue(value) IsNot Nothing Then
                    ddlListField.SelectedValue = value
                End If
            End Set
        End Property

        Public Property PublicFieldValue As String
            Get
                Return FieldValue
            End Get
            Set(ByVal value As String)
                FieldValue = value
            End Set
        End Property

        Public Overrides Function GetFilter() As Business.LuceneFilterQuery
            ' on renvoie une clause booléenne qui prend en compte les différentes possibilités de valeur sélectionnée sur le terme
            Dim toReturn As New LuceneFilterQuery
            Dim BQ As New BooleanQuery()
            For Each value As String In FieldValue.Split(New String() {MultiFieldSeparator}, StringSplitOptions.RemoveEmptyEntries)
                Dim phrase As New PhraseQuery
                phrase.Add(New Term(FieldName, value))
                BQ.Add(phrase, Occur.SHOULD)
            Next
            toReturn.FilterQuery = BQ
            Return toReturn
        End Function

        Public Overrides Function ParseFilter(objFilter As Business.LuceneFilterQuery) As Boolean
            If MyBase.ParseFilter(objFilter) Then
                Return True
            End If
            If TypeOf objFilter.FilterQuery Is BooleanQuery Then
                Dim ValueList As New List(Of String)
                For Each c As BooleanClause In DirectCast(objFilter.FilterQuery, BooleanQuery).GetClauses
                    ' on doit être en SHOULD en terme de clause, et combiner l'ensemble des valeurs
                    'If TypeOf c.GetOccur(). Is BooleanClause.Occur Then

                    'End If
                    '    Return False
                    'End If
                    If TypeOf c.Query Is TermQuery Then
                        Dim TQ As TermQuery = DirectCast(c.Query, TermQuery)
                        Dim CurrentTerm As Term = TQ.Term()
                        If CurrentTerm.Field <> FieldName Then
                            Return False
                        End If
                        ValueList.Add(CurrentTerm.Text)
                    End If

                    If TypeOf c.Query Is PhraseQuery Then
                        Dim TQ As PhraseQuery = DirectCast(c.Query, PhraseQuery)
                        For Each currentTerm As Term In TQ.GetTerms
                            If currentTerm.Field <> FieldName Then
                                Return False
                            End If
                            ValueList.Add(currentTerm.Text)
                        Next
                    End If
                Next
                FieldValue = String.Join(MultiFieldSeparator, ValueList.ToArray)
                Return True
            End If
            Return False
        End Function

#End Region

#Region "Private methods"




        Protected Overridable Sub BindLayout()
            Dim objListTermInfo As List(Of Business.TermInfo) = PortalQueryingDriver.GetCurrent().GetTopTerms(FieldName, NbValues)

            If objListTermInfo.Count <> 0 Then

                If Me.DisplayEmptyItem Then
                    If Not String.IsNullOrEmpty(Me.NullLabel) Then
                        ddlListField.Items.Add(New ListItem(Me.NullLabel, String.Empty))
                    Else
                        ddlListField.Items.Add("")
                    End If
                End If

                For Each termInfo As Business.TermInfo In objListTermInfo

                    Dim TextValue As String = termInfo.Text
                    Dim FI As FieldDefinition = FieldFactory.Instance().GetDefinition(FieldName)
                    If FI.LocalizeValuedInstance Then
                        ' localisé par instance, on cherche la combinaison dans le fichier de ressources
                        Dim ResultValue As String = Localization.GetString(FieldName & "." & TextValue, _
                                NukeHelper.GetModuleSharedResourceFile(Aricie.DNN.Modules.LuceneSearch.Common.Constants.ModuleNameLuceneSearchResults))
                        If (Not String.IsNullOrEmpty(ResultValue)) Then
                            ' si la clé de resource est vide, on garde la version originale, sinon on affiche la resource
                            TextValue = ResultValue
                        End If
                    End If

                    If ToUpper Then
                        TextValue = TextValue.ToUpper
                    End If
                    If (Not String.IsNullOrEmpty(termInfo.Text) AndAlso Not String.IsNullOrEmpty(TextValue)) Then
                        Dim li As New ListItem(TextValue, termInfo.Text)
                        Dim existingLi As ListItem = ddlListField.Items.FindByText(TextValue)
                        If existingLi Is Nothing Then
                            ddlListField.Items.Add(li)
                        Else
                            existingLi.Value = existingLi.Value & MultiFieldSeparator & termInfo.Text
                        End If
                    End If
                Next
                If Not String.IsNullOrEmpty(DefaultItem) Then
                    ddlListField.SelectedValue = DefaultItem
                End If
            Else
                ddlListField.Visible = False
            End If

        End Sub

#End Region

    End Class

End Namespace

